﻿using HLStock.Common;
using HLStock.DBAccess;
using HLStock.Model;
using HLStock.MyExcep;
using HLStock.WmsApi.AuthenticationInjection;
using HLStock.WmsApi;
using HLStock.WmsService;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Net;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace HLStock.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public abstract class MyBaseController<T> : ControllerBase
        where T : BaseModel, new()
    {
        protected const int DEF_PAGE_SIZE = 500;
        protected const int DEF_PAGE_INDEX = 1;
        protected HttpContext httpContext;
        protected DbInstance dbInstance;
        protected abstract BaseServiceProvider<T> getSp();
        public MyBaseController()
        {
            httpContext = ServiceLoader.GetService<IHttpContextAccessor>().HttpContext!;
            dbInstance = ServiceLoader.GetService<DbInstance>();
            dbInstance.StartTransaction();
            httpContext.Response.OnStarting(Func, httpContext);
        }
        protected void Commit()
        {
            this.dbInstance.Commit();
        }
        protected void Abort()
        {
            this.dbInstance.Abort();
        }
        protected Task Func(object obj)
        {
            var context = obj as HttpContext;
            if (context?.Response.StatusCode == (int)HttpStatusCode.OK)
            {
                Commit();
            }
            else
            {
                Abort();
            }
            return Task.CompletedTask;
        }

        [HttpPost, MyAuthorize(AuthorizeLevelEnum.AllUser)]
        public virtual ResponseData<T> List(dynamic? qryParam)
        {
            var index = DEF_PAGE_INDEX;
            var pageSize = DEF_PAGE_SIZE;
            if (qryParam?.pageIndex != null)
                int.TryParse(qryParam?.pageIndex?.ToString(), out index);
            if (qryParam?.pageIndex != null)
                int.TryParse(qryParam?.pageSize?.ToString(), out pageSize);
            var cond = QueryConditionResolver.Resolver<T>(qryParam);
            int total = 0;
            var result = new ResponseData<T>(getSp().List(cond, index, pageSize, out total));
            result.TotalCount = total;
            return result;
        }


        [HttpGet, HttpPost, MyAuthorize(AuthorizeLevelEnum.AllUser)]
        public virtual ResponseData<T> Get(string pk)
        {
            var result = new ResponseData<T>(getSp().Get(pk));
            return result;
        }

        [HttpPost, MyAuthorize(AuthorizeLevelEnum.AllUser)]
        public virtual ResponseData<T> Save(T data)
        {
            getSp().Save(data);
            ResponseData<T> result = new ResponseData<T>(data);
            return result;
        }

        [HttpPost,HttpGet, MyAuthorize(AuthorizeLevelEnum.AllUser)]
        public virtual ResponseData<string> Delete(string pk)
        {
            getSp().Delete(pk);
            ResponseData<string> result = new ResponseData<string>(pk);
            return result;
        }
    }
}
